elasticsearch 配置

前言

Elasticsearch 已经有了很好的默认值配置,如果想更好的发挥其性能,则需要深入了解其配置,对于性能提升是很有帮助的。

位置

elasticsearch-5.6.8/config/elasticsearch.yml

配置

常规配置

# es集群的名称
cluster.name:lipanpan

# 当前节点的名称
node.name:elasticsearch_005_data

# 数据存放目录
path.data:/path/to/data1,/path/to/data2
备注:数据目录支持配置多个,以逗号分隔;最好将数据,日志,插件目录放置到安装目录之外的路径下,防止重新安装Elasticsearch的时候不小心把安装目录覆盖。

# 日志存放目录
path.logs: /path/to/logs

# 插件存放目录
path.plugins: /path/to/plugins

# 是否master候选节点
node.master: true

# 是否数据存储节点
node.data:false

# 绑定ip
network.host: 192.168.1.111
注:设置es服务支持访问的绑定IP

# 是否启用restful访问
http.enabled:true

# restful访问端口
http.port:9200

# restful跨域支持
http.cors.enabled:true
http.cors.allowed.origin: "*"

# 节点间通信端口
transport.tcp.port:9300

索引配置

# 索引的主分片数
index.number_of_shards:5
注:每个索引的主分片数,默认值是 5 。这个配置在索引创建后不能修改。

# 每个主分片的副本数
index.number_of_replicas:1
注:每个主分片的副本数,默认值是1。对于活动的索引库,这个配置可以随时修改。

# 索引刷新间隔时间
index.refresh_interval:1s
注:默认值1s,意味着数据写入1秒后就可以被检索到。

集群发现配置

# 最少的的主节点数
discovery.zen.minimum_master_nodes:2
注:法定个数就是 ( master 候选节点个数 / 2) + 1,少于此值则无法完成选举。

# 集群单播列表
discovery.zen.ping.unicast.hosts:["192.168.1.111","192.168.1.112"]
注:可指定端口,默认是9300;示例:["192.168.1.111:9300"]

# 是否启用多播发现节点
discovery.zen.ping.multicast.enabled:false
注:在同一网段的情况下,可以使用多播发现集群节点;集群名称相同则自动加入该集群,所以使用多播发现节点时,强烈建议使用特殊的集群名称,禁止使用默认的名称elasticsearch,防止新安装的节点自动加入集群,影响数据的分片等;

# 选举通信超时时间
discovery.zen.ping_timeout:5s
注:默认3秒,提高一点以应对网络不好的时候,防止脑裂。

集群恢复配置

# 设置集群中总节点数
gateway.expected_nodes: 10

# 等待集群至少存在8个节点时,启动分片数据恢复
gateway.recover_after_nodes: 8

# 等待5分钟后,启动分片数据恢复
gateway.recover_after_time: 5m

注: 哪个条件先达到,则触发数据恢复。

脑裂

脑裂:集群中同时存在两个master节点的现象;

危害:如果你的集群发生了脑裂,那么你的集群就会处在丢失数据的危险中,因为主节点被认为是这个集群的最高统治者,它决定了什么时候新的索引可以创建,分片是如何移动的等等。如果你有两个masters节点, 你的数据完整性将得不到保证,因为你有两个节点认为他们有集群的控制权。

如果你有两个节点,你遇到难题了。法定数当然是 2 ,但是这意味着如果有一个节点挂掉,你整个集群就不可用了(因为无法进行master选举了)。 设置成1可以保证集群的功能,但是就无法保证集群脑裂了(因为可能存在集群节点间通信问题,导致2个节点各自选举为master节点),像这样的情况,最好至少保证有3个节点。

参考链接

  1. https://www.elastic.co/guide/cn/elasticsearch/guide/current/important-configuration-changes.html
  2. https://www.ibm.com/support/knowledgecenter/zh/SSFPJS_8.5.6/com.ibm.wbpm.main.doc/topics/rfps_esearch_configoptions.html
  3. https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-gateway.html
  4. https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-http.html
  5. https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html
  6. https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-node.html
  7. https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index-modules.html